package com.gamificationlibrary.fragments;


import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.gamificationlibrary.gamificationlibrary.R;
import com.gamificationlibrary.gamificationlibrary.leaderboard.Leaderboard;
import com.gamificationlibrary.gamificationlibrary.model.Player;
import com.gamificationlibrary.view.LeaderboardView;


public class RadioLeaderboardsFragment extends Fragment{

	private ArrayList<Leaderboard> leaderboards;
	private Player player;
	public String[] fieldRadioButton;

	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
		
	    // Inflate the layout for this fragment
		final View rootView=inflater.inflate(R.layout.temporalleaderboards_fragment, container, false);
		
		Bundle bundle=getArguments();
		leaderboards = (ArrayList<Leaderboard>) bundle.get(LeaderboardView.EXTRAS_LEADERBOARD);
        
		player=(Player) bundle.getSerializable(LeaderboardView.EXTRAS_PLAYER);
		
		RadioGroup radioGroup = (RadioGroup) rootView.findViewById(R.id.radioGroup_Leaderboard);
		
		LeaderboardView leaderboardView1=(LeaderboardView) rootView.findViewById(R.id.leaderboardView_Leaderboard);
		leaderboardView1.putContent(leaderboards.get(0), player);
		
		int count = radioGroup.getChildCount();
     
        Boolean findRadio;
        int j=0;
		for(int i=0;i<leaderboards.size();i++)
		{
			findRadio=false;
			while(j<count && !findRadio) {
	            View o = radioGroup.getChildAt(j);
	            j++;
	            if (o instanceof RadioButton) {
	            	findRadio=true;
	            	o.setVisibility(View.VISIBLE);
	        		if(this.fieldRadioButton!=null)
	        		((RadioButton) o).setText(fieldRadioButton[i]);
	        		o.setTag(i);
	            }
	        }
		}
		
		radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() 
	         {
			   public void onCheckedChanged(RadioGroup group, int checkedId) 
			   	{
			         // TODO Auto-generated method stub
				   RadioButton radioButton = (RadioButton) rootView.findViewById(checkedId);
				  
				   if((Integer)radioButton.getTag()==0)
			   	      {  
			   	    	LeaderboardView leaderboardView1=(LeaderboardView) rootView.findViewById(R.id.leaderboardView_Leaderboard);
			   	    	leaderboardView1.putContent(leaderboards.get(0), player);
			   	      }
			   	      else if ((Integer)radioButton.getTag()==1)
			   	    	{
			   	    	LeaderboardView leaderboardView2=(LeaderboardView) rootView.findViewById(R.id.leaderboardView_Leaderboard);
			   	    	leaderboardView2.putContent(leaderboards.get(1), player);
			   	    	}
			   	      else
		   	      		{
			   	    	LeaderboardView leaderboardView2=(LeaderboardView) rootView.findViewById(R.id.leaderboardView_Leaderboard);
			   	    	leaderboardView2.putContent(leaderboards.get(2), player);
			   	    	}
			   	    	  
			        }       
			       });
		return rootView;
    }
	
	
	public void setFieldsRadioButton(String[] fieldsNames)
	{
		this.fieldRadioButton=fieldsNames;
	}
	

}
